﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.Data.Entity;
using TomNet.IMSC.Core.Contracts.NewHouse;
using TomNet.IMSC.Model.DbModels.NewHouse;
using TomNet.IMSC.Web.Areas.NewHouse.Models;
using TomNet.IMSC.Web.Common;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.Security;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Areas.NewHouse.Controllers
{
    public class BuildingStatisticsController : BaseController
    {
        public IBuildingsContract BuildingsContract { get; set; }

        private DefaultDbContext DbContext = new DefaultDbContext();



        //总体统计===================================================================================

        [Description("总体统计")]
        //[GlobalAuthorization]
        public ActionResult NewHouse4TotalIndex()
        {
            return View();
        }

        [Description("总体统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult NewHouse4TotalIndexAsync(int pageIndex, int pageSize,int buildingID, DateTime beginDate, DateTime endDate)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);

            //访问数据库函数
            var query = DbContext.SqlQuery<NewHouse4TotalModel>("select * from [dbo].[Fn_Table_Report_NewHouse4Total](@p0,@p1,@p2,@p3)", loginModel.Id, beginDate, endDate, buildingID);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<NewHouse4TotalModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<NewHouse4TotalModel> gridData = new GridData<NewHouse4TotalModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //楼盘统计===================================================================================

        [Description("楼盘统计")]
        //[GlobalAuthorization]
        public ActionResult NewHouse4BuildingsIndex()
        {
            return View();
        }

        [Description("销售人员统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult NewHouse4BuildingsIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);

            //访问数据库函数
            var query = DbContext.SqlQuery<NewHouse4BuildingsModel>("select * from [dbo].[Fn_Table_Report_NewHouse4Buildings](@p0,@p1,@p2)", loginModel.Id, beginDate, endDate);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<NewHouse4BuildingsModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<NewHouse4BuildingsModel> gridData = new GridData<NewHouse4BuildingsModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //中介统计===================================================================================

        [Description("中介统计")]
        //[GlobalAuthorization]
        public ActionResult NewHouse4IntermediariesIndex()
        { 
            return View();
        }

        [Description("中介统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult NewHouse4IntermediariesIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);

            //访问数据库函数
            var query = DbContext.SqlQuery<NewHouse4IntermediariesModel>("select * from [dbo].[Fn_Table_Report_NewHouse4Intermediaries](@p0,@p1,@p2)", loginModel.Id, beginDate, endDate);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<NewHouse4IntermediariesModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<NewHouse4IntermediariesModel> gridData = new GridData<NewHouse4IntermediariesModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //渠道人员统计===================================================================================

        [Description("渠道人员统计")]
        //[GlobalAuthorization]
        public ActionResult NewHouse4OriginStaffIndex()
        { 
            return View();
        }

        [Description("渠道人员统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult NewHouse4OriginStaffIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);

            //访问数据库函数
            var query = DbContext.SqlQuery<NewHouse4OriginStaffModel>("select * from [dbo].[Fn_Table_Report_NewHouse4OriginStaff](@p0,@p1,@p2)", loginModel.Id, beginDate, endDate);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<NewHouse4OriginStaffModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<NewHouse4OriginStaffModel> gridData = new GridData<NewHouse4OriginStaffModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //销售人员统计===================================================================================

        [Description("销售人员统计")]
        //[GlobalAuthorization]
        public ActionResult NewHouse4SellStaffIndex()
        { 
            return View();
        }

        [Description("销售人员统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult NewHouse4SellStaffIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);

            //访问数据库函数
            var query = DbContext.SqlQuery<NewHouse4SellStaffModel>("select * from [dbo].[Fn_Table_Report_NewHouse4SellStaff](@p0,@p1,@p2)", loginModel.Id, beginDate, endDate);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<NewHouse4SellStaffModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<NewHouse4SellStaffModel> gridData = new GridData<NewHouse4SellStaffModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //报备统计===================================================================================

        [Description("销售人员统计")]
        //[GlobalAuthorization]
        public ActionResult NewHouse4ReportIndex()
        {
            return View();
        }

        [Description("销售人员统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult NewHouse4ReportIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);

            //访问数据库函数
            string sql = "";
            sql += "select NewHouseRunnings.FilingAuditingStaffID, isnull(Staffs.Name,'未知') as StaffName,Count(*) as Number from NewHouseRunnings left outer join Staffs on NewHouseRunnings.FilingAuditingStaffID=Staffs.Id";
            sql += " where [FilingAuditingTime] between '"+ beginDate + "' and '"+ endDate + "' ";
            sql += " group by NewHouseRunnings.FilingAuditingStaffID, Staffs.Name";




            var query = DbContext.SqlQuery<NewHouse4ReportModel>(sql);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.Number);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<NewHouse4ReportModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<NewHouse4ReportModel> gridData = new GridData<NewHouse4ReportModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //楼盘===================================================================================

        [Description("楼盘-列表")]
        //[GlobalAuthorization]
        public ActionResult BuildingsIndex()
        {
            //ViewData["layerindex"] = layerindex;
            return View();
        }

        [Description("楼盘-列表")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult BuildingsIndexAsync(int pageIndex, int pageSize, string Name = "", string Address = "")
        {
            var query = from m in BuildingsContract.Entities.Where(x => !x.IsDeleted) select m;

            //条件搜索
            if (!string.IsNullOrWhiteSpace(Name))
            {
                query = query.Where(m => m.Name.Contains(Name));
            }
            if (!string.IsNullOrWhiteSpace(Address))
            {
                query = query.Where(m => m.Address.Contains(Address));
            }

            //var list = (from m in query
            //            select new UserShow
            //            {
            //                Id = m.Id,
            //                UserName = m.UserName,
            //                PassWord = m.PassWord,
            //                Phone = m.Phone,
            //                DepId = m.DepId,
            //                DepName = (from m1 in DepContract.Entities where m1.Id == m.DepId select m1.DepName).FirstOrDefault(),
            //                PositionName = (from m2 in PosContract.Entities where m2.Id == m.PositionId select m2.Name).FirstOrDefault(),// m.PositionId,
            //                Name = m.Name,
            //                Status = m.Status,
            //                No = m.No,
            //                IsLogin = m.IsLogin,
            //                Authority = m.Authority,
            //                IsLiability = m.IsLiability,
            //                CreatedTime = m.CreatedTime,
            //                IsDeleted = m.IsDeleted,
            //                IsLocked = m.IsLocked,
            //                AttSetName = (from n in AttSetContract.Entities where n.Id == m.AttSetId select n.Name).FirstOrDefault()
            //            });

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.CreatedTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<Buildings>().AsQueryable();// null;

            //此处可以采用匿名对象 GridData<object>
            GridData<object> gridData = new GridData<object>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }
    }
}